﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using DekstopVer.Transfer;
using DekstopVer.FileServis;


namespace DekstopVer
{
    /// <summary>
    /// Interaction logic for Pemesanan.xaml
    /// </summary>
    public partial class Pemesanan : Window
    {

        private string[] stringProduk;
        private string[] stringPaket;
        private FileS file;
        private string fileID;
        private string idMember;


        public Pemesanan(string id)
        {
            InitializeComponent();
            id = idMember;
            fillProdukBox();

            textBox6.IsEnabled = false;
            warnaBox.IsEnabled = false;
            bahan.IsEnabled = false;
            hargaWarna.IsEnabled = false;
            textBox4.IsEnabled = false;
            textBox7.IsEnabled = false;
            textBox5.IsEnabled = false;




        }

        private void fillProdukBox()
        {

            comboBox1.ItemsSource = PesananController.getProduk();

            //stringProduk  = comboBox1.SelectedValue.ToString().Split('|').ToArray();
            // textBox6.Text = stringProduk[2];

        }



        private void fillPaketBox()
        {


            try
            {

                stringProduk = comboBox1.SelectedValue.ToString().Split('|').ToArray();
            }

            catch { return; }
            textBox6.Text = stringProduk[2];
            comboBox2.ItemsSource = PesananController.getPaketbyProduk(stringProduk[0].Trim());





        }



        private void fillDetailBox()
        {

            try
            {
                stringPaket = comboBox2.SelectedValue.ToString().Split('|').ToArray();
            }

            catch { return; }




            string[] paketID = PesananController.getPaketbyID(stringPaket[0].Trim());


            warnaBox.Text = PesananController.getNamaWarna(paketID[0].Trim());
            bahan.Text = PesananController.getNamaBahan(paketID[1].Trim());
            hargaWarna.Text = PesananController.getHargaWarna(paketID[0].Trim());
            textBox4.Text = PesananController.getHargaBahan(paketID[1].Trim());
            textBox7.Text = paketID[2];
            textBox5.Text = paketID[3];

            warnaBox.Text = PesananController.getNamaWarna(paketID[0].Trim());
            bahan.Text = PesananController.getNamaBahan(paketID[1].Trim());
            hargaWarna.Text = PesananController.getHargaWarna(paketID[0].Trim());
            textBox4.Text = PesananController.getHargaBahan(paketID[1].Trim());
            textBox7.Text = paketID[2];
            textBox5.Text = paketID[3];

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

            fillPaketBox();

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            fillDetailBox();
            if (stringProduk[3].ToString().Trim() == "False")
            {
                textBox7.IsEnabled = true;
                textBox5.IsEnabled = true;
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //try
            {

                CetakS cetak = new CetakS();


                cetak.copy = int.Parse(textBox2.Text);
                cetak.lebar = double.Parse(textBox5.Text);

                cetak.panjang = double.Parse(textBox7.Text);

                PesananController.insertCetak(cetak, stringPaket[0].Trim(), fileID);
            }

            //catch (Exception ex) { MessageBox.Show(ex.ToString()); };





        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog dlg = new OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" +
                      "|Office Files|*.doc;*.xls;*.ppt" +
                      "Images (*.bmp;*.jpeg;*.gif)|*.bmp;*.jpeg;*.gif|" +
                      "|All Files|*.*";

            // Display OpenFileDialog by calling ShowDialog method
            dlg.ShowDialog();


            // Open document
            string filename = dlg.FileName;
            FileNameTextBox.Text = filename;


        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            System.IO.FileInfo fileInfo =
                      new System.IO.FileInfo(FileNameTextBox.Text);
            TransferServiceClient clientUpload =
                    new TransferServiceClient();
            DekstopVer.Transfer.RemoteFileInfo
                   uploadRequestInfo = new DekstopVer.Transfer.RemoteFileInfo();

            //try
            {


                using (System.IO.FileStream stream =
                       new System.IO.FileStream(FileNameTextBox.Text,
                       System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    uploadRequestInfo.FileName = fileInfo.Name;
                    uploadRequestInfo.Length = fileInfo.Length;
                    uploadRequestInfo.FileByteStream = stream;



                    clientUpload.UploadFile(uploadRequestInfo.FileName, uploadRequestInfo.Length, uploadRequestInfo.FileByteStream);

                }
            }

            //catch (Exception ex) { MessageBox.Show(ex.ToString()); }

            file = new FileS();

            file.ID = "F";
            file.size = fileInfo.Length;
            file.jenis = fileInfo.Extension;
            file.path = "C:/upload/" + fileInfo.Name;

            fileID = PesananController.insertFile(file);







        }
    }
}
